<template>
    <div class="components-container" v-cloak>
        <sweet-modal ref="dialog" class="dialog" :sweetModalMaxWidth="pageWidth" v-bind:blocking="true">
            <h3>{{title}}</h3>
            <div style="margin:10px 20px">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">业务编号</label>
                        <div class="layui-input-inline">
                            <el-input v-model="crhData.REGNO" placeholder="请输入业务编号" @keyup.enter.native="getRegInfo" :readonly='isINFO'>
                                <el-button slot="append" icon="el-icon-search" @click="getRegInfo"></el-button>
                            </el-input>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">骨灰送回</label>
                        <div class="layui-input-inline">
                            <el-select v-model="REGISSENDBACK" filterable>
                            <el-option v-for="item in isgocome" :key="item.value" :label="item.label" :value="item.value">
                            </el-option>
                            </el-select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">骨灰去向</label>
                        <div class="layui-input-inline">
                             <!-- @change="isShowght($event)" -->
                            <el-select v-model="REGISSENDGO">
                                <el-option :key="item.CODNAME" :label="item.CODALIAS" :value="item.CODNAME" v-for="item in ghqx"></el-option>
                            </el-select>
                        </div>
                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">逝者姓名</label>
                        <div class="layui-input-inline">
                            <el-input v-model="regInfo.REGNAME" readonly></el-input>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">逝者性别</label>
                        <div class="layui-input-inline">
                            <el-input v-model="regInfo.REGSEXDESC" readonly></el-input>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">逝者年龄</label>
                        <div class="layui-input-inline">
                            <el-input v-model="regInfo.REGAGE" readonly></el-input>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">家属性名</label>
                        <div class="layui-input-inline">
                            <el-input v-model="regInfo.REGHANDLERNAME" readonly></el-input>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">逝者关系</label>
                        <div class="layui-input-inline">
                            <el-input v-model="regInfo.REGRELATION" readonly></el-input>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">联系电话</label>
                        <div class="layui-input-inline">
                           <el-input v-model="regInfo.DPOPHONENUM" readonly></el-input>
                        </div>
                    </div>
                </div>
            </div>
            <div :style="{maxHeight:maxHeight+'px'}" style="overflow:auto;">
                <table class="layui-table" style="border-spacing: 0;">
                    <thead>
                        <tr>
                            <th>费用名称</th>
                            <th>单位</th>
                            <th>数量</th>
                            <th>单价</th>
                            <th>金额</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody ref='inputs'>
                        <tr class="allRow" v-for="(chl,index) in chlList">
                            <td>
                                <!-- 费用名称 -->
                                <!-- <el-input v-model="chl.CHLCHAID" readonly></el-input> -->
                                <!-- :readonly='isINFO' -->
                                 <el-select @change="chaSelect($event,index)" ref="select" v-model="chl.CHLCHAID" clearable filterable :readonly='isINFO'>
                                    <el-option v-for="item in CostName" :key="item.CodeValue" :label="item.CodeName" :value="item.CodeValue">
                                    </el-option>
                                </el-select>
                            </td>
                            <td>
                                <!-- 单位 -->
                                <el-input v-model="chl.CHLAUNIT" readonly>
                                </el-input>
                            </td>
                            <td>
                                <!-- 数量 -->
                                <el-input @keyup.native="check(index)" v-model="chl.CHLNUM" :readonly='isINFO'>
                                </el-input>
                            </td>
                            <td>
                                <!-- 单价 -->
                                <el-input @keyup.native="check(index)" v-model="chl.CHLMONEY"></el-input> 
                            </td>
                            <td>
                                <!-- 金额 -->
                                <el-input v-model="chl.CHLMOUNT" readonly></el-input>
                                <!-- {{chl.CHLMOUNT}} -->
                            </td>
                            <td>
                                <button class="layui-btn layui-btn-small layui-btn-normal" @click="remove(index)" :disabled='isINFO'>
                                    <i class="layui-icon">&#xe640;</i>
                                </button>
                            </td>
                        </tr>
                    </tbody>
                    <tfoot>
                        <tr>
                            <td colspan="4"></td>
                            <td>{{ mountTotal }}</td>
                            <td>
                                <button class="layui-btn layui-btn-small layui-btn-normal" @click="insert()" :disabled='isINFO'>
                                    <i class="layui-icon">&#xe654;</i>
                                </button>
                            </td>
                        </tr>
                    </tfoot>
                </table>
            </div>
            <div class="layui-form-item btnCenter sweet-btn">
                <el-button @click="closeDialog">取消</el-button>
                <!-- <el-button @click="print">寄存条码打印</el-button> -->
                <!-- <el-button @click="gojicun" v-if="this.title=='寄存费用新增' || this.title=='寄存费用修改'" :loading="this.$store.state.isloading">开始寄存</el-button> -->
                <el-button type="primary" :disabled='this.issubmit' @click="submissions()" v-if="this.title=='寄存费用新增' || this.title=='寄存费用修改'" :loading="this.$store.state.isloading">提交</el-button>
            </div>
        </sweet-modal>
        <assUseMain ref="assUseMain"></assUseMain>
        <sweet-modal ref="Print" class="dialog" :sweetModalMaxWidth="pageWidth">
            <h3>{{titles}}</h3>
            <iframe :src="printUrl" style="width:1100px; height:500px;margin-top:20px;"></iframe>
        </sweet-modal>
    </div>
</template>
<style rel="stylesheet/scss" lang="scss" scoped>
@import "src/styles/common.scss";
.el-button + .el-button {
  margin-left: 0;
}

.sweet-btn {
  margin-top: 20px;
}
</style>

<script>
import sweetModal from "@/components/sweetModal";
import apiBase from "@/api/apiBase.js";
import { formatDate, timeStringToDate } from "@/utils/date.js";
import { accMul, Subtr, accAdd } from "@/utils/operations.js";
import { ORDER } from "@/const/orderConst.js";
import { PRINT } from "@/components/Pdf/PDF.js";
import assUseMain from "@/views/server/assUseMain";

const ADD = "新增";
const INFO = "义乌详情";
const CHANGE = "修改";

export default {
  name: "hello",
  data() {
    return {
      pageWidth: 1200,
      maxHeight: 500,
      order: null,
      title: null,
      titles: null,
      reginfoid: null, //regid
      REGISSENDBACK: "A",
      REGISSENDGO: null,
      issubmit: false,
      ghqx: [],
      //收费信息
      regInfo: {
        DPOUSENAM: null, //业务编号
        REGNAME: null, //逝者姓名
        REGSEXDESC: null, //逝者性别
        REGAGE: null, //年龄
        DPGID: null,
        // REGADDRESS: null,//接尸地点
        REGHANDLERNAME: null, //家属姓名
        REGRELATION: null, //逝者关系
        // DPOUSENAM: null,//使用人
        DPOPHONENUM: null //联系电话
      },
      crhData: {
        REGNO: null //业务编号
      },
      //收费项目
      chlList: [],
      chl: {
        DOLDEPOSIT: null, //所属仓库
        // DOLDPGID: null,//物品类别编号
        DOLDPGID: "", //物品类别编号
        DOLIDESOK: null, //库存数量
        CHLNUM: null, //出库数量
        CHLMOUNT: null //出库金额
      },
      DPOID: null,
      CostName: [], //收费项目
      isINFO: null,
      isgocome: [{ value: "B", label: "是" }, { value: "A", label: "否" }],
      printUrl: null
    };
  },
  methods: {
    //删除行
    remove(index) {
      this.chlList.splice(index, 1);
    },
    // print(){
    //     let chlList = JSON.parse(JSON.stringify(this.chlList));
    //     if (chlList.length > 0) {
    //         chlList.forEach((item,index) => {
    //             if(item.CHLCHAID == null){
    //                 chlList.splice(index,1);
    //             }
    //         });
    //     };
    //     chlList.forEach(item => {
    //         item.CHLCHAID = item.CHLCHAID; //id
    //         item.CHLAUNIT = item.CHLAUNIT; //单位
    //         item.CHLMONEY = item.CHLMONEY; //单价
    //         item.CHLNUM = item.CHLNUM; //数量
    //         item.CHLMOUNT = item.CHLMOUNT; //金额
    //     });
    //     apiBase.DoService(
    //         {
    //             DPCREGID: this.reginfoid,
    //             list_Chl: chlList,
    //             REGISSENDBACK: this.REGISSENDBACK,
    //             REGGHQX: this.REGISSENDGO
    //         },
    //         apiBase.getApiAdress().DepositCost.Create,
    //         res => {
    //             //打印
    //             this.titles = '打印逝者信息';
    //             let data = {
    //                 REGID:this.reginfoid
    //             };
    //             apiBase.DoService(
    //             data,
    //             apiBase.getApiAdress().Pdf.PrintRegDetail,
    //             res => {
    //                 this.printUrl = res.data.content.PDFNAME;
    //                 this.$refs.Print.open();
    //             });
    //             this.refreshData();
    //             // this.DPOID = res.data.content.DPOID;
    //         }
    //     );
    // },
    print() {
      let data = {
        REGID: this.reginfoid
      };
      apiBase.DoService(
        data,
        apiBase.getApiAdress().Pdf.PrintAssDetail,
        res => {
          this.printUrl = res.data.content.PDFNAME;
          this.$refs.Print.open();
        }
      );
    },
    gojicun() {
      this.$refs.assUseMain.open("jicun", "", this.crhData.REGNO);
    },
    //入库时候的提交
    submissions() {
      this.$confirm("确认是否提交该数据?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(callback => {
          let chlList = JSON.parse(JSON.stringify(this.chlList));
          if (chlList.length > 0) {
            chlList.forEach((item, index) => {
              if (item.CHLCHAID == null) {
                chlList.splice(index, 1);
              }
            });
          }
          chlList.forEach(item => {
            item.CHLCHAID = item.CHLCHAID; //id
            item.CHLAUNIT = item.CHLAUNIT; //单位
            item.CHLMONEY = item.CHLMONEY; //单价
            item.CHLNUM = item.CHLNUM; //数量
            item.CHLMOUNT = item.CHLMOUNT; //金额
          });
          apiBase.DoService(
            {
              DPCREGID: this.reginfoid,
              list_Chl: chlList,
              REGISSENDBACK: this.REGISSENDBACK,
              REGGHQX: this.REGISSENDGO
            },
            apiBase.getApiAdress().DepositCost.Create,
            res => {
              this.$message({
                type: "success",
                message: "成功!"
              });
              this.issubmit = true;
              this.refreshData();
              // this.DPOID = res.data.content.DPOID;
            }
          );
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消"
          });
        });
      // this.refreshData();
    },
    //获取编码
    getCode() {
      let data = {};
      apiBase.DoService(
        data,
        apiBase.getApiAdress().Registration.QueryAllCode,
        res => {
          this.codeList = res.data.content;
          for (var i in this.codeList) {
            switch (this.codeList[i].CTPID) {
              case "骨灰去向":
                this.ghqx = this.codeList[i].Cods;
                break;
              default:
            }
          }
        }
      );
    },
    //获取收费项目
    QuerySok() {
      let data = {
        SOURCE: "寄存费用"
      };
      apiBase.DoService(
        data,
        apiBase.getApiAdress().Checkout.GetChargeBySource,
        res => {
          this.CostName = res.data.content;
          this.CostName.forEach(item => {
            item.CodeValue = item.CHAID;
            item.CodeName = item.CHANAME;
          });
        }
      );
    },
    //收费项目改变触发
    chaSelect(index, num) {
      let costdata = {};
      for (let i in this.CostName) {
        if (this.CostName[i].CodeValue == index) {
          costdata = this.CostName[i];
        }
      }
      this.chlList[num].CHLAUNIT = costdata.CHAAUNIT;
      this.chlList[num].CHLMONEY = costdata.CHAMONEY;
      this.chlList[num].CHLNUM = 1;
      this.chlList[num].CHLMOUNT = costdata.CHAMONEY;
    },

    datareapet() {
      Object.assign(this.$data.regInfo, this.$options.data().regInfo);
      this.REGISSENDBACK = "A";
      this.REGISSENDGO = null;
      this.crhData.REGNO = null;
    },
    //打开弹出框
    open(order, id) {
      this.order = order;
      this.DPOID = id;
      this.issubmit = false;
      this.getCode();
      switch (this.order) {
        case ADD:
          this.title = "寄存费用新增";
          this.chlList = [];
          this.isINFO = false;
          this.datareapet();
          this.insert(); //新增行
          this.QuerySok(); //获取收费项目
          this.$refs.dialog.open();
          break;
        case INFO:
          this.title = "寄存费用详情";
          this.chlList = [];
          this.isINFO = true;
          setTimeout(() => {
            this.getRegInfo();
          }, 100);
          this.QuerySok(); //获取收费项目
          this.crhData.REGNO = id;
          this.$refs.dialog.open();
          break;
        case CHANGE:
          this.title = "寄存费用修改";
          this.chlList = [];
          this.isINFO = false;
          setTimeout(() => {
            this.getRegInfo();
          }, 100);
          this.QuerySok(); //获取收费项目
          this.crhData.REGNO = id;
          this.$refs.dialog.open();
          break;
        default:
      }
    },
    //新增行
    insert() {
      // for (let index in this.chlList) {
      //     let chl = this.chlList[index];
      //     if (chl.CHLCHAID.length === 0) {
      //         this.$message({
      //         type: "error",
      //         message: `第${parseInt(index) + 1}行收费项目不能为空!`
      //         });
      //         return false;
      //     }
      // }
      this.chlList.push(JSON.parse(JSON.stringify(this.chl)));
      return true;
    },
    //根据正式业务编号获取ID
    getRegInfo() {
      let data = {
        REGNO: this.crhData.REGNO
      };
      apiBase.DoService(
        data,
        apiBase.getApiAdress().Registration.GetRegistrationDepositByNo,
        res => {
          let reg = res.data.content;
          if (!reg) {
            this.$message({
              type: "error",
              message: "未找到逝者信息!"
            });
            Object.assign(this.$data.regList, this.$options.data().regList);
            return;
          }
          this.chlList = res.data.content.list_Chl;
          if (this.chlList.length > 0) {
            this.chlList.forEach(item => {
              item.DPGID = item.DOLDPGID;
            });
          } else {
            this.insert();
          }
          this.regInfo = res.data.content;
          this.reginfoid = res.data.content.REGID;
          this.regInfo.DPOUSENAM = res.data.content.REGHANDLERNAME;
          this.regInfo.REGRELATION = res.data.content.REGRELATION;
          this.regInfo.DPOPHONENUM = res.data.content.DPOPHONENUM;
          this.REGISSENDGO = res.data.content.REGGHQX;
        }
      );
    },

    check(index) {
      let chls = this.chlList[index];
      chls.CHLMOUNT = accMul(Number(chls.CHLNUM), Number(chls.CHLMONEY));
    },
    //只能输入正整数
    checkNumber(value) {
      value = value.replace(/\D/g, "").replace(/^0+(?=\d)/, "");
      if (value !== "") {
        if (value.substr(0, 1) === "0" && value.length === 2) {
          value = value.substr(1, value.length);
        }
      } else {
        value = 0;
      }
      return value;
    },
    //只能输入金额
    checkMoney(value) {
      //修复第一个字符是小数点 的情况.
      if (value !== "" && value.substr(0, 1) === ".") {
        value = "";
      }
      value = value.replace(/^0*(0\.|[1-9])/, "$1"); //解决 粘贴不生效
      value = value.replace(/[^\d.]/g, ""); //清除“数字”和“.”以外的字符
      value = value.replace(/\.{2,}/g, "."); //只保留第一个. 清除多余的
      value = value
        .replace(".", "$#$")
        .replace(/\./g, "")
        .replace("$#$", ".");
      value = value.replace(/^(\-)*(\d+)\.(\d\d).*$/, "$1$2.$3"); //只能输入两个小数
      if (value.indexOf(".") < 0 && value !== "") {
        //以上已经过滤，此处控制的是如果没有小数点，首位不能为类似于 01、02的金额
        if (value.substr(0, 1) === "0" && value.length === 2) {
          value = value.substr(1, value.length);
        }
      } else if (value === "") {
        value = 0;
      }
      return value;
    },
    closeDialog() {
      switch (this.order) {
        case ADD:
        case INFO:
        case CHANGE:
          this.$refs.dialog.close();
          break;
        default:
          break;
      }
    },
    refreshData() {
      this.$emit("refreshData");
    }
  },
  created() {},
  //计算属性
  computed: {
    mountTotal() {
      let total = 0;
      for (let chl of this.chlList) {
        total = accAdd(total, chl.CHLMOUNT);
      }
      return total;
    }
  },

  watch: {},
  mounted() {},
  components: {
    sweetModal,
    assUseMain
  }
};
</script>
